package com.example.handler;

import com.alibaba.fastjson.JSONObject;
import com.example.base.BaseApiService;

import com.example.entity.AppEntity;
import com.example.mapper.AppMapper;
import com.example.utils.BaseRedisService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 *  验证accessToken
 */

@Component
public class AccessTokenInterceptor extends BaseApiService implements HandlerInterceptor {

    @Autowired
    private BaseRedisService redisService;
    @Resource
    private AppMapper appMapper;
    /**
     *  进入  controller层之前进行拦截
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("-----------开始进入拦截请求---------------------");
        String accessToken=request.getParameter("accessToken");
        //1.获取对应的accessToken
        if (StringUtils.isEmpty(accessToken)){
            resultError("accessToken is null",response);
            return false;
        }
        //2.使用accessToken对应的value，如果没有对应的appId 直接返回 错误提示(无效token)
        String appId= (String) redisService.getString(accessToken);
        if(StringUtils.isEmpty(appId)){
            resultError("accessToken is invalid",response);
            return false;
        }
        //3.如果使用accessToken查询redis，查询到对应的appId，使用appId查询对应的信息  从数据库中
        AppEntity result=appMapper.findAppId(appId);
        if (result==null){
            resultError("not found app info",response);
            return false;
        }
        //4.判断 该机构是否被授权使用 isFlag，如果为1 则无权限调用，为1  则可以调用
        if (result.getIsFlag()==1){
           resultError("not permission ",response);
           return false;
        }
        //进行正常的业务操作。
        return true;
    }


    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("--------------------------------处理请求完成后视图渲染之前的处理操作----------------------------------------------");
    }


    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("---------------------------------视图渲染之后的操作------------------------------------");
    }

    //返回错误提示
    public void resultError(String errorMsg,HttpServletResponse response){
        try {
            PrintWriter printWriter = response.getWriter();
            printWriter.write(new JSONObject().toJSONString(setResultError(errorMsg)));
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
